package com.syni.mdd.sjb.community.base.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.syni.mdd.sjb.common.component.utils.common.utils.RestTemplateUtils;
import com.syni.mdd.sjb.common.component.utils.common.utils.StringUtils;
import lombok.extern.log4j.Log4j2;

import java.util.*;

@Log4j2
public class CommonUtils {



    public static Set<String> list(String userId) {
        Set<String> externalUserids = new HashSet<>();
        String accessToken = OAUtils.getAccessToken();
        String postUrl = String.format("https://qyapi.weixin.qq.com/cgi-bin/externalcontact/list?access_token=%s&userid=%s",accessToken,userId);
        String responseBody = RestTemplateUtils.getForEntity(postUrl);
        log.info("CustomerUtils_list：" + responseBody);
        if(StringUtils.isNotBlank(responseBody)){
            JSONObject jsonObject = JSON.parseObject(responseBody);
            String[] userList = (String[]) jsonObject.get("external_userid");
            externalUserids.addAll(new HashSet<>(Arrays.asList(userList)));
        }
        return externalUserids;
    }

    public static Set<Integer> getDepartments(String accessToken) {
        Set<Integer> departments = new HashSet<>();
        String getUrl = String.format("https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token=%s",accessToken);
        String responseBody = RestTemplateUtils.getForEntity(getUrl);
        log.info("getDepartments：" + responseBody);
        if(StringUtils.isNotBlank(responseBody)){
            JSONObject jsonObject = JSON.parseObject(responseBody);
            String errcode = jsonObject.getString("errcode");
            if("0".equals(errcode)){
                JSONArray department = jsonObject.getJSONArray("department");
                if(department.size() > 0){
                    for(int ii=0;ii < department.size();ii++){
                        departments.add(department.getJSONObject(ii).getInteger("id"));
                    }
                }
            }

        }
        return departments;
    }

    public static Set<String> getSimplelist(String accessToken,Integer departId){
        Set<String> userIds = new HashSet<>();
        String getUrl = "https://qyapi.weixin.qq.com/cgi-bin/user/simplelist";
        Map<String,Object> paramsMap = new HashMap<>();
        paramsMap.put("access_token",accessToken);
        paramsMap.put("department_id",departId);
        String responseBody = RestTemplateUtils.getForEntity(getUrl,paramsMap);
        log.info("getSimplelist：" + responseBody);
        if(StringUtils.isNotBlank(responseBody)){
            JSONObject jsonObject = JSON.parseObject(responseBody);
            String errcode = jsonObject.getString("errcode");
            if("0".equals(errcode)){
                JSONArray jsonArray = jsonObject.getJSONArray("userlist");
                if(jsonArray.size() > 0){
                    for(int ii=0;ii < jsonArray.size();ii++){
                        userIds.add(jsonArray.getJSONObject(ii).getString("userid"));
                    }
                }
            }
        }
        return userIds;
    }

    public static Set<String> getExternalcontactList(String accessToken, String userId) {
        Set<String> externalUserid = new HashSet<>();
        String getUrl = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/list";
        Map<String,Object> paramsMap = new HashMap<>();
        paramsMap.put("access_token",accessToken);
        paramsMap.put("userid",userId);
        String responseBody = RestTemplateUtils.getForEntity(getUrl,paramsMap);
        log.info("getExternalcontactList：" + responseBody);
        if(StringUtils.isNotBlank(responseBody)){
            JSONObject jsonObject = JSON.parseObject(responseBody);
            String errcode = jsonObject.getString("errcode");
            if("0".equals(errcode)){
                List<String> externalUserids = (List<String>) jsonObject.get("external_userid");
                if(externalUserids.size() > 0){
                    for(int ii = 0;ii < externalUserids.size();ii++){
                        externalUserid.add(externalUserids.get(ii));
                    }
                }
            }
        }
        return externalUserid;
    }

    public static String getExternalcontactDetail(String accessToken, String ecId) {
        String getUrl = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get";
        Map<String,Object> paramsMap = new HashMap<>();
        paramsMap.put("access_token",accessToken);
        paramsMap.put("external_userid",ecId);
        String responseBody = RestTemplateUtils.getForEntity(getUrl,paramsMap);
        log.info("getExternalcontactDetail：" + responseBody);
        if(StringUtils.isNotBlank(responseBody)){
            JSONObject jsonObject = JSON.parseObject(responseBody);
            String errcode = jsonObject.getString("errcode");
            if("0".equals(errcode)){
                return jsonObject.toJSONString();
            }
        }
        return "";
    }
    /**
     * 拆分集合
     *
     * @param <T>
     * @param resList 要拆分的集合
     * @param count   每个集合的元素个数
     * @return 返回拆分后的各个集合
     */
    public static <T> List<List<T>> split(List<T> resList, int count) {
        if (resList == null || count < 1)
            return null;
        List<List<T>> ret = new ArrayList<List<T>>();
        int size = resList.size();
        if (size <= count) { //数据量不足count指定的大小
            ret.add(resList);
        } else {
            int pre = size / count;
            int last = size % count;
            //前面pre个集合，每个大小都是count个元素
            for (int i = 0; i < pre; i++) {
                List<T> itemList = new ArrayList<T>();
                for (int j = 0; j < count; j++) {
                    itemList.add(resList.get(i * count + j));
                }
                ret.add(itemList);
            }
            //last的进行处理
            if (last > 0) {
                List<T> itemList = new ArrayList<T>();
                for (int i = 0; i < last; i++) {
                    itemList.add(resList.get(pre * count + i));
                }
                ret.add(itemList);
            }
        }
        return ret;
    }

}
